package com.xy.bizportdemo.task;

import android.content.Context;
import android.util.Log;

/**
 * Created by admin on 2016/12/28.
 */

public class LoadTaskManager {
    private static final String TAG = "LoadTaskManager";

    protected Context mContext = null;
    private OnLoadListener mOnSystemLoadListener = null;
    private OnLoadListener mOnPathLoadListener = null;
    private LoadSmsAsyncTask mLoadSmsAsyncTask = null;
    private LoadBaseAsyncTask mLoadPathAsyncTask = null;

    private static LoadTaskManager sInstance;
    public static synchronized LoadTaskManager getInstance(Context context) {
        if (sInstance == null) {
            sInstance = new LoadTaskManager(context.getApplicationContext());
        }

        return sInstance;
    }

    private LoadTaskManager(Context context) {
        mContext = context;
    }

    public void setOnSystemLoadListener(OnLoadListener listener) {
        mOnSystemLoadListener = listener;
        if (mLoadSmsAsyncTask != null) {
            mLoadSmsAsyncTask.setOnLoadCallback(mOnSystemLoadCallback);
        }
    }

    public void setOnPathLoadListener(OnLoadListener listener) {
        mOnPathLoadListener = listener;
        if (mLoadPathAsyncTask != null) {
            mLoadPathAsyncTask.setOnLoadCallback(mOnPathLoadCallback);
        }
    }

    private LoadBaseAsyncTask.OnLoadCallback mOnSystemLoadCallback = new LoadBaseAsyncTask.OnLoadCallback() {
        @Override
        public void onProgress(int current, int total) {

        }

        @Override
        public void onStatus(int status) {
            if (mOnSystemLoadListener != null) {
                mOnSystemLoadListener.onStatus(status);
            }
        }
    };

    private LoadBaseAsyncTask.OnLoadCallback mOnPathLoadCallback = new LoadBaseAsyncTask.OnLoadCallback() {
        @Override
        public void onProgress(int current, int total) {

        }

        @Override
        public void onStatus(int status) {
            if (mOnPathLoadListener != null) {
                mOnPathLoadListener.onStatus(status);
            }
        }
    };

    public void executeSystem() {
        mLoadSmsAsyncTask = new LoadSmsAsyncTask(mContext);
        mLoadSmsAsyncTask.setOnLoadCallback(mOnSystemLoadCallback);
        mLoadSmsAsyncTask.execute("");
    }

    public void executePath(String path) {
        if (mLoadPathAsyncTask != null) {
            mLoadPathAsyncTask.cancel(true);
            mLoadPathAsyncTask = null;
        }

        if (path.toLowerCase().endsWith(".xml")) {
            LoadXmlAsyncTask task = new LoadXmlAsyncTask(mContext);
            task.setPath(path);
            mLoadPathAsyncTask = task;
        } else if (path.toLowerCase().endsWith(".xls")) {
            LoadXlsAsyncTask task = new LoadXlsAsyncTask(mContext);
            task.setPath(path);
            mLoadPathAsyncTask = task;
        } else {
            Log.w(TAG, "executePath: " + path);
        }

        if (mLoadPathAsyncTask != null) {
            mLoadPathAsyncTask.setOnLoadCallback(mOnPathLoadCallback);
            mLoadPathAsyncTask.execute("");
        }
    }

    public interface OnLoadListener {
        void onProgress(int current, int total);
        void onStatus(int status);
    }
}
